#include <Windows.h>
#include <tchar.h>
#include <assert.h>

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK _HookWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	TCHAR szTemp[256];
	_stprintf_s(szTemp, _T("[HookProc msg] => hWnd: 0x%06X  nMsg: %06X  wParam: 0x%06X  lParam: 0x%06X\n"),
				hWnd, nMsg, wParam, lParam);
	OutputDebugString(szTemp);

	WNDPROC _oldWndProc = (WNDPROC)GetWindowLongPtr(hWnd, GWLP_USERDATA);
	if (NULL == _oldWndProc)	
	{
		_oldWndProc = (WNDPROC)GetClassLongPtr(hWnd, GCLP_WNDPROC);
	}
	return _oldWndProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT ID_BTNHOOKMSG	= 1001;
	static const UINT ID_LABINFO	= 1003;
	
	static HWND hButton;
	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);

			CreateWindow(_T("Button"), _T("Start Hook"), WS_VISIBLE|WS_CHILD, 0, 0, 150, 50, hWnd, (HMENU)ID_BTNHOOKMSG, 0, 0);
			CreateWindow(_T("STATIC"), _T(""), WS_VISIBLE|WS_CHILD, 0, 60, 800, 400, hWnd, (HMENU)ID_LABINFO, 0, 0);

			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case ID_BTNHOOKMSG:
				{
					static bool bHook = false;

					if (!bHook)
					{
						TCHAR szTmp[MAX_PATH];
						TCHAR szClsName[MAX_PATH];
						GetClassName(hWnd, szClsName, MAX_PATH);
						WNDCLASSEX wcx	= {0};
						wcx.cbSize		= sizeof(WNDCLASSEX);
						
						SetWindowText(GetDlgItem(hWnd, ID_BTNHOOKMSG), _T("Stop Hook"));
						OutputDebugString(_T("\n============== Start Hook Message ================\n"));
						
						GetClassInfoEx(GetModuleHandle(NULL), szClsName, &wcx);
						_stprintf_s(szTmp, _T("WndProc Value before SetWindowLongPtr: 0x%0X  ==> 0x%0X\n"), wcx.lpfnWndProc, GetWindowLongPtr(hWnd, GWLP_WNDPROC));
						OutputDebugString(szTmp);

						SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)WndProc);
						SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG)_HookWndProc);

						GetClassInfoEx(GetModuleHandle(NULL), szClsName, &wcx);
						_stprintf_s(szTmp, _T("WndProc Value After SetWindowLongPtr: 0x%0X  ==> 0x%0X\n"), wcx.lpfnWndProc, GetWindowLongPtr(hWnd, GWLP_WNDPROC));
						OutputDebugString(szTmp);
						OutputDebugString(_T("==================================================================\n"));
						bHook = true;
					} else {
						SetWindowText(GetDlgItem(hWnd, ID_BTNHOOKMSG), _T("Start Hook"));
						OutputDebugString(_T("\n============== Stop Hook Message ================\n"));
						SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)0);
						SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG)WndProc);
						bHook = false;
					}

					break;
				}

			}
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	TCHAR szTemp[256];
	_stprintf_s(szTemp, _T("[WndProc msg] => hWnd: 0x%06X  nMsg: %06X  wParam: 0x%06X  lParam: 0x%06X\n"),
		hWnd, nMsg, wParam, lParam);
	OutputDebugString(szTemp);

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
								0, 0, 800, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0029");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}